package com.gudaomai.wuye.web;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.CommRegion;
import com.gudaomai.wuye.entity.CommRegionInput;
import com.gudaomai.wuye.entity.Layout;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.mapper.CommRegionMapper;
import com.gudaomai.wuye.service.ICommRegionServ;
import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/commRegion")
public class CommRegionCtrl extends BaseController {

	@Autowired
	ICommRegionServ commRegionService;

	/**
	 * Save an new commRegion
	 * 
	 * 1. Check if all required field are filled in. 2. Check if all the inputs
	 * are valid. 3. Check if the commRegion with the same properties does not
	 * exist in the DB. 4. Save the inputed values.
	 * 
	 * @param
	 * @author jess return
	 *
	 **/
	@ApiOperation("The commRegion settings page, click the add button, add commRegion")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public CommRegion addCommRegion(@RequestBody CommRegionInput commRegionInput) throws CommonException {

		CommRegion commRegionEntity;

		if (commRegionService.isExist(commRegionInput.getCode())) {
			// The commoRegion already exists in the database

			throw new CommonException(CommonConstant.RESULT_CODE_CREATED,
					getMessage(MsgConstant.RET_COMMREGION_ADD_FAILURE));
		} else {
			// Create new commRegion entity
			commRegionEntity = new CommRegion();
			// copy all user inputs to the new commRegion entity
			BeanUtils.copyProperties(commRegionInput, commRegionEntity);

			// Insert the commRegion entity into the database
			commRegionService.insert(commRegionEntity);

			// Return message and HTTP result code
			commRegionEntity.setRetCode(CommonConstant.RESULT_CODE_OK);
			commRegionEntity.setRetMessage(getMessage(MsgConstant.RET_COMMREGION_ADD_SUCCESS));

		}

		return commRegionEntity;
	}

	/**
	 * update the existing commRegion
	 * 
	 * 1. Check if all required field are filled in. 2. Check if all the inputs
	 * are valid. 3. update the commRegion in the DB.
	 * 
	 * @param
	 * @author jess return
	 *
	 **/

	@ApiOperation("The commRegion settings page, click the modify button, update commRegion")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public CommRegion updateCommRegion(@RequestBody CommRegionInput commRegionInput) throws Exception {

		// update the commRegion entity in the database
		CommRegion commRegionEntity = new CommRegion();

		// copy all user inputs to the new commRegion entity
		BeanUtils.copyProperties(commRegionInput, commRegionEntity);

		commRegionService.updateById(commRegionEntity);

		// Return message and HTTP result code
		commRegionEntity.setRetCode(CommonConstant.RESULT_CODE_OK);
		commRegionEntity.setRetMessage(getMessage(MsgConstant.RET_COMMREGION_UPDATE_SUCCESS));

		return commRegionEntity;
	}

	/**
	 * delete the existing commRegion 1. Check if the corresponding Id exists in
	 * the DB. 2. Delete the commRegion from the DB.
	 * 
	 * @param id
	 * @author jess return
	 *
	 **/
	@ApiOperation("The commRegion settings page, click the delete button, delete commRegion")
	@RequestMapping(value = "/id/{id}", method = RequestMethod.DELETE)
	public RetEntity delCommRegionById(@PathVariable("id") String id) throws Exception {

		RetEntity entity = new RetEntity();

		commRegionService.deleteById(id);

		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_COMMREGION_DELETE_SUCCESS));
		return entity;
	}


	@RequestMapping(value = "/house", method = RequestMethod.GET)

	public List<CommRegion> getAllCommHouse() throws Exception {

		List<CommRegion> houses = commRegionService.getAllCommHouse();

		return houses;
	}


	@RequestMapping(value = "/park", method = RequestMethod.GET)

	public List<CommRegion> getAllCommPark() throws Exception {

		List<CommRegion> parks = commRegionService.getAllCommPark();

		return parks;
	}


	@RequestMapping(value = "/store", method = RequestMethod.GET)

	public List<CommRegion> getAllCommStore() throws Exception {

		List<CommRegion> stores = commRegionService.getAllCommStore();

		return stores;
	}

}
